package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.LoanAuthTypeList;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jiangzx on 15:43.
 * 贷款授权类型adapter
 */

public class LoanAuthTypeAdapter extends CommonRecyclerAdapter<LoanAuthTypeList.LoanAuthType>{
    private static final int COUNT_PER_ROW=2;//每行2个
    private RecycleItemClickedCallback clickedCallback;

    public void setClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    public LoanAuthTypeAdapter(Context context){
        super(context,null,R.layout.item_image_text_text);
    }

    @Override
    public void convert(final CommonRecyclerHolder holder, LoanAuthTypeList.LoanAuthType loanAuthType) {
        int position=holder.getAdapterPosition();
        View rootView=holder.getView(R.id.rl_root);
        //先确定行数
        int line=position/2;
        int lineMode=line%5;
        switch (lineMode){
            case 0:
                if(position%2==0){
                    rootView.setBackgroundResource(R.mipmap.img_line1_left);
                }else{
                    rootView.setBackgroundResource(R.mipmap.img_line1_right);
                }
                break;
            case 1:
                if(position%2==0){
                    rootView.setBackgroundResource(R.mipmap.img_line2_left);
                }else{
                    rootView.setBackgroundResource(R.mipmap.img_line2_right);
                }
                break;
            case 2:
                if(position%2==0){
                    rootView.setBackgroundResource(R.mipmap.img_line3_left);
                }else{
                    rootView.setBackgroundResource(R.mipmap.img_line3_right);
                }
                break;
            case 3:
                if(position%2==0){
                    rootView.setBackgroundResource(R.mipmap.img_line4_left);
                }else{
                    rootView.setBackgroundResource(R.mipmap.img_line4_right);
                }
                break;
            case 4:
                if(position%2==0){
                    rootView.setBackgroundResource(R.mipmap.img_line5_left);
                }else{
                    rootView.setBackgroundResource(R.mipmap.img_line5_right);
                }
                break;
        }
        holder.setTextViewText(R.id.tv_title,loanAuthType.getName());
        holder.setTextViewText(R.id.tv_content,loanAuthType.getDescription());
        ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+loanAuthType.getPicture_path(),
                (CircleImageView)holder.getView(R.id.civ),
                R.mipmap.img_default_square);
        if(isLastRow(position)){
            try{
                ViewGroup.MarginLayoutParams layoutParams=
                        (ViewGroup.MarginLayoutParams) holder.getView(R.id.rl_root).getLayoutParams();
                layoutParams.bottomMargin= DensityUtil.dp2px(15);
                holder.getView(R.id.rl_root).setLayoutParams(layoutParams);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedCallback!=null){
                    clickedCallback.onRecycleItemClicked(holder.position);
                }
            }
        });
    }

    private boolean isLastRow(int position){
        boolean isLastRow=false;
        int itemCount=getItemCount();
        int lines;//总行数
        if(itemCount%COUNT_PER_ROW==0){
            lines=itemCount/COUNT_PER_ROW;
        }else{
            lines=itemCount/COUNT_PER_ROW+1;
        }
        if((position/COUNT_PER_ROW)+1==lines){
            isLastRow=true;
        }
        return isLastRow;
    }
}
